package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.GxClinic;
import com.ruoyi.system.domain.GxPatient;
import com.ruoyi.system.service.IGxPatientService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 患者Controller
 *
 * @author gx
 * @date 2022-05-07
 */
@RestController
@RequestMapping("/system/patient")
public class GxPatientController extends BaseController
{
    @Autowired
    private IGxPatientService gxPatientService;

    /**
     * 查询患者列表
     */
    @GetMapping("/list")
    public TableDataInfo list(GxPatient gxPatient)
    {
        startPage();
        List<GxPatient> list = gxPatientService.selectGxPatientList(gxPatient);
        return getDataTable(list);
    }

    @GetMapping("/archiveList")
    public TableDataInfo archiveList(GxPatient gxPatient)
    {
        startPage();
        List<GxPatient> list = gxPatientService.selectArchiveList(gxPatient);
        return getDataTable(list);
    }

    /**
     * 导出患者列表
     */
//    @PreAuthorize("@ss.hasPermi('system:patient:export')")
    @Log(title = "患者", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxPatient gxPatient)
    {
        List<GxPatient> list = gxPatientService.selectGxPatientList(gxPatient);
        ExcelUtil<GxPatient> util = new ExcelUtil<GxPatient>(GxPatient.class);
        util.exportExcel(response, list, "患者数据");
    }

    /**
     * 获取患者详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:patient:query')")
    @GetMapping(value = "/{patientId}")
    public AjaxResult getInfo(@PathVariable("patientId") Long patientId)
    {
        return AjaxResult.success(gxPatientService.selectGxPatientByPatientId(patientId));
    }

    /**
     * 新增患者
     */
//    @PreAuthorize("@ss.hasPermi('system:patient:add')")
    @Log(title = "患者", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GxPatient gxPatient)
    {
        Long patientId = gxPatient.getPatientId();
        AjaxResult ajax = AjaxResult.success();
        return toAjax(gxPatientService.insertGxPatient(gxPatient));
    }

    @PostMapping("/insert")
    public AjaxResult insert(@RequestBody GxPatient gxPatient)
    {
        gxPatientService.insertGxPatient(gxPatient);
        Long patientId = gxPatient.getPatientId();
        System.out.println(patientId);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("patientId",patientId);
//        return toAjax(gxPatientService.insertGxPatient(gxPatient));
        return ajax;
    }

    /**
     * 修改患者
     */
//    @PreAuthorize("@ss.hasPermi('system:patient:edit')")
    @Log(title = "患者", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxPatient gxPatient)
    {
        return toAjax(gxPatientService.updateGxPatient(gxPatient));
    }

    /**
     * 删除患者
     */

    @Log(title = "患者", businessType = BusinessType.DELETE)
    @DeleteMapping("/{patientIds}")
    public AjaxResult remove(@PathVariable Long[] patientIds)
    {
        return toAjax(gxPatientService.deleteGxPatientByPatientIds(patientIds));
    }
}
